//
//  DCTaskDetailViewController.m
//  JiuyaoMotorcade
//
//  Created by 大橙子 on 2018/11/29.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCTaskDetailViewController.h"
#import "DCTaskDetailHeaderView.h"
#import "DCTaskDetailTableViewCell.h"
#import "DCTaskDetailFooterView.h"
#import "DCGisticsView.h"
#import "DCGisticsModel.h"
//#import "DCZhisunViewController.h"
#import "DCTaskDetailModel.h"
//#import "DCTaskDetailOddArray.h"
//#import "DCTransportErrorDetailController.h"
@interface DCTaskDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *tableFooterView;
@property (nonatomic,copy) NSString *tuskNum;
@property (nonatomic,copy) NSString *state;
@property (nonatomic,strong) DCTaskDetailModel *detailModel;

@end

@implementation DCTaskDetailViewController
-(instancetype)initWithOrderID:(NSString *)tuskNum  state:(NSString *)state
{
    if (self = [super init]) {
        self.tuskNum = tuskNum;
        self.state = state;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"任务详情";
//    NSDictionary *dict = @{
//                           @"carNumber":@"1",
//                           @"carrier":@"九耀",
//                           @"damageMark":@"0",
//                           @"taskCode":@"EDKJDHKSDHGU213857486",
//                           @"oddArray":@[
//                                   @{
//                                       @"carNumber":@"1",
//                                       @"endAddress":@"莆田区",
//                                       @"endCity":@"上海市",
//                                       @"endDate":@"2018-11-27 00:00:00",
//                                       @"id":@"2c9180876753986f0167546e7687007b",
//                                       @"orderStatus":@"15",
//                                       @"carArray":@[
//                                               @{
//                                                   @"carType":@"",
//                                                   @"color":@"",
//                                                   @"vIN":@"FDF4343646",
//                                                   },
//                                               @{
//                                                   @"carType":@"",
//                                                   @"color":@"",
//                                                   @"vIN":@"FDF4343646",
//                                                   },
//                                               @{
//                                                   @"carType":@"",
//                                                   @"color":@"",
//                                                   @"vIN":@"FDF4343646",
//                                                   },
//                                               ],
//                                       @"receiverArray":@[
//                                               @{
//                                                   @"name":@"4444",
//                                                   @"phoneNumber":@"13548474444",
//                                                   }
//                                               ],
//                                       @"startAddress":@"顺义仁和大街",
//                                       @"startCity":@"北京市",
//                                       @"suvNumber":@"2",
//                                       @"totalNumber":@"3",
//                                       @"transportCode":@"FSE3346636743",
//                                       },
//                                   @{
//                                       @"carNumber":@"1",
//                                       @"endAddress":@"莆田区",
//                                       @"endCity":@"上海市",
//                                       @"endDate":@"2018-11-27 00:00:00",
//                                       @"id":@"2c9180876753986f0167546e7687007b",
//                                       @"orderStatus":@"15",
//                                       @"carArray":@[
//                                               @{
//                                                   @"carType":@"",
//                                                   @"color":@"",
//                                                   @"vIN":@"FDF4343646",
//                                                   },
//                                               @{
//                                                   @"carType":@"",
//                                                   @"color":@"",
//                                                   @"vIN":@"FDF4343646",
//                                                   },
//                                               @{
//                                                   @"carType":@"",
//                                                   @"color":@"",
//                                                   @"vIN":@"FDF4343646",
//                                                   },
//                                               ],
//                                       @"receiverArray":@[
//                                               @{
//                                                   @"name":@"4444",
//                                                   @"phoneNumber":@"13548474444",
//                                                   }
//                                               ],
//                                       @"startAddress":@"顺义仁和大街",
//                                       @"startCity":@"北京市",
//                                       @"suvNumber":@"2",
//                                       @"totalNumber":@"3",
//                                       @"transportCode":@"FSE3346636743",
//                                       },
//                                   @{
//                                       @"carNumber":@"1",
//                                       @"endAddress":@"莆田区",
//                                       @"endCity":@"上海市",
//                                       @"endDate":@"2018-11-27 00:00:00",
//                                       @"id":@"2c9180876753986f0167546e7687007b",
//                                       @"orderStatus":@"15",
//                                       @"carArray":@[
//                                               @{
//                                                   @"carType":@"",
//                                                   @"color":@"",
//                                                   @"vIN":@"FDF4343646",
//                                                   },
//                                               @{
//                                                   @"carType":@"",
//                                                   @"color":@"",
//                                                   @"vIN":@"FDF4343646",
//                                                   },
//                                               @{
//                                                   @"carType":@"",
//                                                   @"color":@"",
//                                                   @"vIN":@"FDF4343646",
//                                                   },
//                                               ],
//                                       @"receiverArray":@[
//                                               @{
//                                                   @"name":@"4444",
//                                                   @"phoneNumber":@"13548474444",
//                                                   }
//                                               ],
//                                       @"startAddress":@"顺义仁和大街",
//                                       @"startCity":@"北京市",
//                                       @"suvNumber":@"2",
//                                       @"totalNumber":@"3",
//                                       @"transportCode":@"FSE3346636743",
//                                       }
//
//                                   ],
//                           @"orderTrack":@{
//                                   @"nodeArray":@[
//                                           @{
//                                               @"name":@"aname",
//                                               @"time":@"2018-11-27 17:10:48",
//                                               },
//                                           @{
//                                               @"name":@"aname",
//                                               @"time":@"2018-11-27 17:10:48",
//                                               },
//                                           @{
//                                               @"name":@"aname",
//                                               @"time":@"2018-11-27 17:10:48",
//                                               },
//                                           @{
//                                               @"name":@"aname",
//                                               @"time":@"2018-11-27 17:10:48",
//                                               },
//                                           @{
//                                               @"name":@"aname",
//                                               @"time":@"2018-11-27 17:10:48",
//                                               },
//                                           ],
//                                   @"total":@"5",
//                                   },
//    };

    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, self.view.width, 50)];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = FONT(25);
    self.titleLabel.textColor = BlueColor;
    self.titleLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.titleLabel];
    
//    self.detailModel = [DCTaskDetailModel mj_objectWithKeyValues:dict];
//    self.titleLabel.text = self.detailModel.taskCode;
    
//    [self.view addSubview:self.tableView];
    
    [self getDetailData];
}
-(void)getDetailData {
    __weak typeof(self) weakself = self;
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"taskId"] = self.tuskNum;
    [DCServiceTool postWithUrl:getOrderDetail_URL params:info success:^(id responseObject) {
        if ([responseObject[@"cdoe"] integerValue] == 0) {
            DCLog(@"responseObject---%@",responseObject);
            weakself.detailModel = [DCTaskDetailModel mj_objectWithKeyValues:responseObject[@"record"]];
            weakself.titleLabel.text = self.detailModel.taskCode;
            
            [weakself.view addSubview:weakself.tableView];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.titleLabel.bottom+5, ScreenWidth, self.view.height-self.titleLabel.height - 10 - bottomSafeHeight) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellStyleDefault;
        _tableView.backgroundColor = BackgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = self.tableFooterView;
    }
    return _tableView;
}
-(UIView *)tableFooterView {
    if (!_tableFooterView) {
        NSArray *dataSource = [DCGisticsModel mj_objectArrayWithKeyValuesArray:self.detailModel.orderTrack[@"nodeArray"]];
        _tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, dataSource.count*40+40+25)];
        _tableFooterView.backgroundColor = BackgroundColor;
        
        DCGisticsView *gisticsView = [[DCGisticsView alloc]initWithSourceData:dataSource state:self.state];
        gisticsView.backgroundColor = [UIColor whiteColor];
        gisticsView.frame = CGRectMake(0, 5, _tableFooterView.width, (dataSource.count+1)*40);
        [_tableFooterView addSubview:gisticsView];
    }
    return _tableFooterView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.detailModel.oddArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    DCTaskDetailOddArray *model = self.detailModel.oddArray[section];
    return model.carArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 200;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    DCTaskDetailHeaderView *headerView = [[DCTaskDetailHeaderView alloc]init];
    headerView.model = self.detailModel.oddArray[section];
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    DCTaskDetailFooterView *footerView = [[DCTaskDetailFooterView alloc]init];
    footerView.model = self.detailModel.oddArray[section];
    return footerView;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DCTaskDetailTableViewCell *cell = [DCTaskDetailTableViewCell cellWithTableView:tableView];
    DCTaskDetailOddArray *model = self.detailModel.oddArray[indexPath.section];
    cell.model = model.carArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

//-(void)footerBtnDidClick:(UIButton *)button
//{
//    if (button.tag == 0) {
//        DCZhisunViewController *zhisunVC = [[DCZhisunViewController alloc]init];
//        zhisunVC.taskNumber = self.tuskNum;
//        [self.navigationController pushViewController:zhisunVC animated:YES];
//    }else{
//        DCTransportErrorDetailController *transPort = [[DCTransportErrorDetailController alloc]init];
//        transPort.taskNumber = self.tuskNum;
//        [self.navigationController pushViewController:transPort animated:YES];
//    }
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
